﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LibrarySolution.ComMethod;
using LibrarySolution.DAO.基础;
using LibrarySolution.DAO.委托;
using LibrarySolution.Model.基础;

namespace LibrarySolution.Frm
{
    public partial class FrmGlyxx : Form
    {
        #region 属性
        GlyDao glyDao = new GlyDao();
        private static FrmGlyxx Instance;
        Delegates delegates = new Delegates();
        #endregion

        #region 构造器
        public FrmGlyxx()
        {
            InitializeComponent();
            delegates.refreshLBEvent += new Delegates.refreshLBEventHandler(delegates_refreshLBEvent);
            refreshDtgv();
        }  
        #endregion

        #region 事件

        public static FrmGlyxx GetInstance()
        {
            if (Instance == null || Instance.IsDisposed)
            {
                Instance = new FrmGlyxx();
                Instance.MdiParent = GlobalVar.frmMain;
                GlobalVar.frmMain.panel1.Controls.Add(Instance);
            }
            Instance.BringToFront();
            return Instance;
        }

        private void TstripDel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要删除选中项?","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Exclamation,
                MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes)
            {
                if (dataGridView1.CurrentCell.RowIndex != 0)
                {
                    string glyCode = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[0].Value.ToString();
                    glyDao.delSelectRow(glyCode);
                    refreshDtgv();
                }
                else
                    MessageBox.Show("无法删除超级管理员");
            }
        }

        private void TstripBtnRef_Click(object sender, EventArgs e)
        {
            refreshDtgv();
        }

        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            String glyCode;
            GlyEntity glyEntity = new GlyEntity();
            glyCode = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[0].Value.ToString();
            glyEntity.gly_code = glyCode;
            GlobalVar.GlyEntity= glyDao.getGlyxxByCode(glyEntity);
            FrmGlyxxModify.IsModify = true;
            FrmGlyxxModify.GetInstance(delegates).ShowDialog();
        }

        private void TstripBtnAdd_Click(object sender, EventArgs e)
        {
            FrmGlyxxModify.IsModify = false;
            FrmGlyxxModify.GetInstance(delegates).ShowDialog();
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 2)
            {
                if (e.Value != null && e.Value.ToString().Length > 0)
                    e.Value = new string('*', e.Value.ToString().Length);
            }
        }
        #endregion

        #region 方法
        void delegates_refreshLBEvent()
        {
            refreshDtgv();
        }

        void refreshDtgv()
        {
            DataTable dt = null;
            GlyEntity glyEntity = new GlyEntity();
            dt = glyDao.getDataTable(glyEntity);
            dataGridView1.DataSource = dt;
            dataGridView1.Columns[0].Visible = false;
            dataGridView1.Refresh();
        }
        #endregion
    }
}
